<?php

namespace app\admin\controller;

use app\common\model\Cost as cs;
use think\Controller;
use think\Request;
use app\common\model\SiteConfig;
use app\admin\model\Admin;
use app\common\model\Adver;

class System extends Controller
{
    /**
     * 系统管理 类 
     */

    //网站设置
    public function index()
    {
        //获取站点基本信息
        $site = new SiteConfig;
        $cost = new \app\common\model\Cost();
        $info = $site->getinfo();
        $cost_info = $cost->getinfo();
        //判断操作
        if ($this->request->isPost()) {
            $site_info = $this->request->post();
            if ($site->upinfo($site_info)) {
                return json_x(0, '更新成功');
            }
            return json_x(-1, '更新失败');
        } else {
            //输出模板元素
            $list = ['info' => $info, 'cost' => $cost_info];
            return $this->fetch('admin/main/setting', $list);
        }
    }
    //管理员设置
    //管理员修改密码
    public function admin()
    {
        if ($this->request->isPost()) {
            $date = $this->request->param();
            $ad = new Admin();
            if ($ad->update_pass($date['old_pass'], $date['new_pass'])) {
                return json_x(0, '修改成功');
            } else {
                return json_x(-1, '修改失败');
            }
        } else {
            return $this->fetch('admin/main/update_pass');
        }
    }

    //注册设置
    public function regsite(){
        $cost = new cs();
        if ($this->request->isPost()) {
            $content = $this->request->param();
            if ($cost->upinfo($content)) {
                return json_x(0, '修改成功');
            }
            return json_x(-1, '修改失败');
        } else {
            $info = $cost->getinfo(0);
            return $this->fetch('admin/main/regsite',['info'=>$info]);
        }
    }

    //广告设置
    public function ad_index()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param();
            if ($data['type'] == 'start') {
                if (Adver::where('Id', $data['id'])->update(['state' => 1])) {
                    return json_x(0, '成功');
                } else {
                    return json_x(-1);
                }
            }
            if ($data['type'] == 'stop') {
                if (Adver::where('Id', $data['id'])->update(['state' => 0])) {
                    return json_x(0, '成功');
                } else {
                    return json_x(-1);
                }
            }
            if ($data['type'] == 'up') {
                $a = new Adver;
                if ($a->up_ad($data)) {
                    return json_x(0, '成功');
                } else {
                    return json_x(-1);
                }
            }
        }

        return $this->fetch('admin/main/ad_list');
    }
    public function adlist()
    {
        $ad = new Adver;
        //输出广告管理列表
        $page = $this->request->param("page") ? $this->request->param("page") : 1;
        $page = intval($page);
        $limit = $this->request->param("limit") ? $this->request->param("limit") : 1;
        $limit = intval($limit);
        $start = $limit * ($page - 1);
        $q_list = $ad->getlist($start, $limit);
        return $q_list;
    }
    public function ad_edit()
    {
        $id =  request()->param('id');
        $info = Adver::where('Id', $id)->find();

        $this->assign('info', $info);
        return $this->fetch('admin/main/ad_edit');
    }
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move('../public/uploads/');
        if ($info) {
            return json_img($info->getSaveName());
        } else {
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }
}
